package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleContentService;
import com.heima.article.service.ApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.service.ApAuthorService;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-10-19
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ApAuthorService apAuthorService;

    @Autowired
    private ApArticleContentService contentService;
    @Override
    public Long saveArticle(WmNewsResultDTO dto) {

        LambdaQueryWrapper<ApArticle> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ApArticle::getWmNewsId, dto.getWmUserId());
        ApArticle apArticle = getOne(lqw);
        if (apArticle == null) {

            ApAuthor apAuthor = apAuthorService.findByWmUserId(dto.getWmUserId());
            if(apAuthor!=null){
                apArticle=new ApArticle();
                apArticle.setAuthorId(apAuthor.getId());
                apArticle.setAuthorName(apAuthor.getName());
            }

            apArticle.setLikes(0L);
            apArticle.setCollection(0L);
            apArticle.setComments(0L);
            apArticle.setViews(0L);
            apArticle.setCreatedTime(new Date());
            apArticle.setIsComment(1);
            apArticle.setIsForward(1);
            apArticle.setIsDelete(1);
            apArticle.setIsDown(0);
            apArticle.setWmNewsId(dto.getId());
            apArticle.setFlag(0);
            //保存
        }
        apArticle.setTitle(dto.getTitle());
        apArticle.setChannelId(dto.getChannelId());
        apArticle.setChannelName(dto.getChannelName());
        apArticle.setLayout(dto.getType());
        apArticle.setImages(dto.getImages());
        apArticle.setLabels(dto.getLabels());
        apArticle.setPublishTime(dto.getPublishTime()==null? new Date():dto.getPublishTime());
        boolean b = saveOrUpdate(apArticle);

        if (b != true) {
            log.error("保存或者新增失败");
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        Long articleId = apArticle.getId();
//        ap_article表和 ap_article_content表 是1 对1
//        保存article_content表
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setArticleId(articleId);
        apArticleContent.setContent(dto.getContent());
        boolean b1 = contentService.saveOrUpdate(apArticleContent);
        if(!b1){
            log.error("保存aparticle_content表失败！");
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }
        return articleId;
    }
}
